package com.michaelbulava.TFS.ui.dialogs.workspaces;

import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.project.Project;
import com.michaelbulava.TFS.Messages;
import com.michaelbulava.TFS.tasks.workspaces.CreateWorkspaceTask;
import com.michaelbulava.TFS.tasks.workspaces.UpdateWorkspaceTask;
import com.michaelbulava.TFS.ui.controls.workspaces.WorkspaceData;
import com.michaelbulava.TFS.ui.controls.workspaces.WorkspaceEditControl;
import com.michaelbulava.TFS.ui.dialogs.BaseDialog;
import com.microsoft.tfs.core.TFSTeamProjectCollection;
import com.microsoft.tfs.core.clients.versioncontrol.soapextensions.Workspace;
import com.microsoft.tfs.util.Check;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;
import java.text.MessageFormat;

/**
 * Created by mbulava on 7/16/2015.
 */
public class WorkspaceEditDialog extends BaseDialog {
    private final boolean forEdit;
    private Workspace workspace;
    private WorkspaceData workspaceData;
    private final TFSTeamProjectCollection connection;
    private boolean immutable = false;
    private WorkspaceEditControl workspaceEditControl;


    public WorkspaceEditDialog(@NotNull Component parent, Workspace workspace) {
        super(parent);

        //Check.notNull(workspace, "workspace");
        Check.notNull(workspace, "connection");
        //Check.isTrue(((workspace == null && !forEdit) || true), "workspace can only be new when creating new Workspace");
        //Check.isTrue(((workspace != null && forEdit) || true), "workspace cannot be null.");
        this.forEdit = true;
        this.workspace = workspace;
        this.connection = workspace.getClient().getConnection();

        this.workspaceEditControl = new WorkspaceEditControl(this.connection);
        this.workspaceEditControl.setWorkspaceData(new WorkspaceData(workspace));

        myInit();
        pack();

    }

    public WorkspaceEditDialog(@NotNull Component parent, WorkspaceData workspaceData, TFSTeamProjectCollection connection){
        super(parent);
        Check.notNull(workspaceData, "workspaceData");
        Check.notNull(connection, "connection");

        this.forEdit = false;
        this.workspaceData = workspaceData;
        this.connection = connection;
        this.workspaceEditControl = new WorkspaceEditControl(this.connection);
        this.workspaceEditControl.setWorkspaceData(workspaceData);
        myInit();
        this.pack();
        this.doValidate();
    }



    protected WorkspaceEditDialog(@Nullable Project project, boolean forEdit, Workspace workspace, TFSTeamProjectCollection connection) {
        super(project);

        Check.notNull(workspace, "workspace");
        Check.notNull(connection, "connection");

        this.forEdit = forEdit;
        this.workspace = workspace;
        this.connection = connection;
        this.workspaceEditControl = new WorkspaceEditControl(this.connection);

        this.pack();
        myInit();
    }


    @Override
    protected void postInit(){
        this.workspaceEditControl.setAdvancedButtonClickListener(new WorkspaceEditControl.AdvancedButtonClickListener() {
            @Override
            public void onClick() {
                WorkspaceEditDialog.this.repaint();
                pack();
            }
        });
    }

    @Override
    protected JComponent createControl() {
        if (workspaceEditControl == null){
            this.workspaceEditControl = new WorkspaceEditControl(this.connection);
            workspaceEditControl.setWorkspaceData(new WorkspaceData(workspace));
        }
        return workspaceEditControl;
    }

    @Override
    protected String dialogTitle() {
        if (this.forEdit)
        {
            if (workspace.getName() != null) {
                String messageFormat = Messages.getString("WorkspaceEditDialog.EditWorkspaceDialogTitleFormat");
                return MessageFormat.format(messageFormat, new Object[]{ workspace.getName() });
            }
            return Messages.getString("WorkspaceEditDialog.EditWorkspaceDialogTitle");
        }
        return Messages.getString("WorkspaceEditDialog.AddWorkspaceDialogTitle");
    }

    @Override
    protected Action[] createActions() {
        return defaultActions();
    }

    private void test(){
        super.doOKAction();
    }

    @Override
    protected void doOKAction(){
//        IValidity validity = this.workspaceEditControl.getWorkingFolderDataTable().validate();
//        if (!validity.isValid())
//        {
//            String messageFormat = Messages.getString("WorkspaceEditDialog.ErrorDialogTextFormat");
//            String message = MessageFormat.format(messageFormat, new Object[] { validity.getFirstMessage().getMessage() });
//            Messages.showErrorDialog(workspaceEditControl, message, null);
//            //MessageBoxHelpers.errorMessageBox(getShell(), null, message);
//            return;
//        }
        Task task = null;
        WorkspaceData dataToEdit = workspaceEditControl.getWorkspaceData();
        if (forEdit){
            //Update Workspace
            task = new UpdateWorkspaceTask(workspace, dataToEdit.getWorkspaceDetails().getName(),
                    dataToEdit.getWorkspaceDetails().getComment(),
                    dataToEdit.getWorkingFolderDataCollection().createWorkingFolders(),
                    dataToEdit.getWorkspaceDetails().getWorkspaceOptions(),
                    dataToEdit.getWorkspaceDetails().getWorkspaceLocation(),
                    dataToEdit.getWorkspaceDetails().getPermissionProfile());
        } else {
            //Create Workspace
            task = new CreateWorkspaceTask(this.connection, dataToEdit.getWorkingFolderDataCollection().createWorkingFolders(),
                    dataToEdit.getWorkspaceDetails().getName(), dataToEdit.getWorkspaceDetails().getComment(),
                    dataToEdit.getWorkspaceDetails().getWorkspaceLocation(),
                    dataToEdit.getWorkspaceDetails().getWorkspaceOptions(),
                    dataToEdit.getWorkspaceDetails().getPermissionProfile());
        }

        try{
            ProgressManager.getInstance().run(task);
        } catch(Throwable tr){
            Messages.showErrorDialog(workspaceEditControl, tr.getLocalizedMessage(), "Error updating workspace");
            return;
        }


        if (forEdit){
            workspace = ((UpdateWorkspaceTask)task).getWorkspace();
        } else {
            workspace = ((CreateWorkspaceTask)task).getWorkspace();
        }

        super.doOKAction();
    }

    public Workspace getWorkspace(){
        return this.workspace;
    }
}
